package com.mialab.servlet_demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/RequestLineServlet")
public class RequestLineServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	public RequestLineServlet() {
		super();		
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置响应到客户端MIME类型和字符编码方式
		response.setContentType("text/html;charset=UTF-8");
		// 获取请求使用的HTTP方法
		String method = request.getMethod();
		// 获取请求行中的资源名部分
		String uri = request.getRequestURI();
		// 获取使用的协议及版本号
		String protocol = request.getProtocol();
		// 获取请求URL后面的查询字符串
		String queryString = request.getQueryString();
		// 获取Servlet所映射的路径
		String servletPath = request.getServletPath();
		// 获取请求资源所属于的Web应用的路径
		String contextPath = request.getContextPath();

		// 获取输出流
		PrintWriter out = response.getWriter();
		out.println("<p>请求使用的HTTP方法：" + method + "</p>");
		out.println("<p>请求行中的资源名部分：" + uri + "</p>");
		out.println("<p>请求使用的协议及版本号：" + protocol + "</p>");
		out.println("<p>请求URL后面的查询字符串：" + queryString + "</p>");
		out.println("<p>Servlet所映射的路径：" + servletPath + "</p>");
		out.println("<p>请求资源所属于的Web应用的路径：" + contextPath + "</p>");		
		out.append("Served at：").append(request.getContextPath());
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		doGet(request, response);
	}

}
